package cn.wx.scholar.neo4j.repository;

import java.util.Set;

import org.neo4j.ogm.model.Result;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
/*import org.springframework.data.neo4j.repository.GraphRepository;*/
import org.springframework.data.repository.query.Param;
/*import org.springframework.stereotype.Repository;*/
import cn.wx.scholar.neo4j.entity.Neo4jPaper;

//@Repository
public interface PaperRepository extends Neo4jRepository<Neo4jPaper,Long>{
	
	/**
	 * 论文分页查询
	 * @param expertId
	 * @param docType
	 * @param pageable
	 * @return
	 */
	@Query(value = "match(e:expert)-[r:publish1]-(p:paper) where ID(e)={expertId} and p.docType={docType} return p order by p.fPublishDate desc",
			countQuery="match(e:expert)-[r:publish1]-(p:paper) where ID(e)={expertId} and p.docType={docType} return count(ID(p))")
	Page<Neo4jPaper> getPapersByExpertId(@Param("expertId") Long expertId,
			@Param("docType") String docType,
			Pageable pageable);
	
	@Query("MATCH (n:paper) WHERE ID(n) = {id} WITH n MATCH p=(n)-[*0..1]-(m) RETURN p")
	Neo4jPaper findPaperDetailById(@Param("id") Long id);

	/**
	 * 根据领域id查询论文
	 * @param fos
	 * @return
	 * 
	 */
	@Query("call apoc.index.search(\"locationspaper\",{fosName},1000000) yield node as p ,weight as w with p return count(ID(p)) as pac ,p.year as pYear order by p.year")
	Result statisticsPaperByPos(@Param("fosName") String fosName);

	/**
	 * 查询当前人的领域
	 * @param fos
	 * @param id
	 * @return
	 */
	@Query(" unwind {fosNames} as x  "
	+ " call apoc.index.search(\"locationspaper\",x,1000000) yield node as p ,weight as w with p,x "
	+ " match(e:expert)-[r:publish1]->(pa:paper) where ID(e)={expertId} and ID(pa)=ID(p) "
	+ " return distinct x, pa.year as year,count(pa) as num ")
	Result statisticsPaperByExpertPos(@Param("fosNames")Set<String> fosNames, @Param("expertId")Long expertId);
	
	
	@Query("match(e:expert)-[r:publish1]->(pa:paper) where ID(e)={expertId} return ID(pa) as paperId,pa.fos as fos,pa.year as year")
	Result statisticsPaperByExpertPos(@Param("expertId")Long expertId);
}